const app = getApp()
const Controllers = require('../controllers/controllers').controller
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'我的收藏',
    list: [],
    isShowSelect: true, //是否显示选择按钮
    isShowSingleSelect: true, //是否点击当个选择按钮
    currentId: null, //当前歌曲的id
    lastPage: null,
    pageNumber: 1,
    totalRow: 0,
    multipleList: [], //存储选中的id
    isSelected: 0,
    isMultipled: false, //是否已全选
  },
  // 多选按钮
  selectHandle() {
    this.setData({
      isShowSelect: !this.data.isShowSelect
    })
  },
  // 单个选择按钮
  singleSelectBtn(e) {
    let id = parseInt(e.currentTarget.dataset.id);
    let isMultipled = this.data.isMultipled; //是否全选
    let isSelected = this.data.isSelected; //已选多少个
    let multipleList = this.data.multipleList; //存储选中的id
    let list = this.data.list;
    // 判断是否已经选中
    if (multipleList.includes(id)) {
      // 选中的，就直接删除掉
      let idx = multipleList.indexOf(id);
      multipleList.splice(idx, 1)
      isSelected--
      isMultipled = false
    } else {
      multipleList.push(id)
      // 判断是否全选
      if (multipleList.length == list.length) {
        isMultipled = true
      }
      isSelected++
    }


    console.log('单个选择按钮', id, isSelected)
    for (let item of list) {
      if (multipleList.includes(item.collectId)) {
        item.select = true
      } else {
        item.select = false
      }
    }
    this.setData({
      isShowSingleSelect: true,
      currentId: id,
      multipleList,
      isSelected,
      isMultipled,
      list
    })
  },

  // 全选按钮
  multipleSelectBtn() {
    console.log('全选按钮')
    let multipleList = this.data.multipleList; //存储选中的id
    let isMultipled = this.data.isMultipled; //是否全选
    let isSelected = this.data.isSelected; //已选多少个
    let list = this.data.list;
    if (multipleList.length != 0) {
      if (multipleList.length == list.length) {
        multipleList.length = 0
        isMultipled = false
        isSelected = 0
        list.map(item => {
          item.select = false
        })
      } else {
        list.map(item => {
          if (!item.select) {
            item.select = true
            multipleList.push(item.collectId)
          }
        })
        isMultipled = true
        isSelected = list.length
      }
    } else {
      list.map(item => {
        item.select = true
        multipleList.push(item.collectId)
      })
      isMultipled = true
      isSelected = list.length
    }
    this.setData({
      multipleList,
      isMultipled,
      isSelected,
      list
    })
  },
  // 单个下载
  singleDownload(e) {
    let rid = e.currentTarget.dataset.id 
    Controllers.download({
      rid
    }).then(res => {
      console.log('单个下载', res)
      if (res.code == 0) {
        wx.showToast({
          title: '下载成功',
          duration: 1000
        })
      }
    })
  },
  // 多个用户取消收藏按钮
  multipledUnsubscribeBtn() {
    let that = this
    let multipleList = this.data.multipleList
    let str = multipleList.join(',')
    console.log(typeof str, str)
    if (multipleList.length != 0) {
      // 弹出窗
      that.Dialog.ShowDialog({
        title: '是否取消收藏的音频',
        type: 'Confirm',
        callback(obj) {
          console.log('eee', obj)
          if (obj.name == 'confirm') {
            Controllers.delCollect({
                ids: str
              })
              .then(res => {
                console.log(res)
                if (res.code == 0) {
                  that.setData({
                    isShowSelect: true,
                    isSelected: 0,
                    multipleList: [],
                    pageNumber: 1,
                    list: [],
                    isMultipled: false
                  })
                  wx.showToast({
                    title: '取消收藏成功',
                    duration: 1000
                  })
                  that.getCollectListData();
                }
              }).catch(err => {
                console.log(err)
                wx.showToast({
                  title: err.data.msg,
                  duration: 1000,
                  icon: 'none'
                })
              })
          }
          that.Dialog.CloseDialog();
          that.setData({
            isShowSelect: true
          })
        }
      })
    } else {
      wx.showToast({
        title: '请选择收藏记录',
        icon: 'none',
        duration: 1000
      })
    }
  },
  // 获取收藏列表数据
  getCollectListData() {
    let that = this
    Controllers.myCollectList({
        page: that.data.pageNumber,
        token: wx.getStorageSync('logintoken')
      })
      .then(res => {
        console.log('获取收藏列表数据', res)

        if (res.code == 0 && res.data.baseData && res.data.baseData.list && res.data.baseData.list.length > 0) {
          that.setData({
            list: that.data.list.concat(res.data.baseData.list),
            lastPage: res.data.baseData.lastPage,
            pageNumber: res.data.baseData.pageNumber,
            totalRow: res.data.baseData.totalRow
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onLoad')
    if (!wx.getStorageSync('logintoken')) {
      this.login();
    } else {
      this.getCollectListData();
    }
  },
  login() {
    app.GetLogin()
  },
  listenBtn(e) {
    console.log('听按钮', e)
    let {
      rid,
      type
    } = e.currentTarget.dataset
    let url = ''
    // 听按钮 1是栏目，2是合辑，3是音频，4是达人榜，5是评论
    if (type == 1) {
      url = "/pages/Exclusive/Exclusive?id=" + rid
    } else if (type == 2) {
      url = "/pages/Album/Detail/Detail?id=" + rid
    } else if (type == 3) {
      url = '/pages/Playing/Playing?audioId=' + rid
    } else if (type == 4) {
      url = '/pages/Talent/Other/Other?id=' + rid
    } else if (type == 5) {
      url = '/pages/Comment/Comment?rid=' + rid + "&type=" + type
    }
    wx.navigateTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.Dialog = this.selectComponent('#Dialog');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow')

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (!that.data.lastPage) {
      // 存在数据
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })
      console.log('存在数据', that.data.pageNumber)
      that.getCollectListData();

    } else {
      wx.showToast({
        title: '没有更多了',
        icon:"none"
      })
    }
  },

  // 分享按钮
  shareBtn(e) {
    wx.navigateTo({
      url: '/pages/AudioSharing/AudioSharing?type=7&id=' + e.currentTarget.dataset.id + '&title=' + this.data.currentSong.title,
    })
  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·我的收藏',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·我的收藏',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})